
package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.HospitalWarehouse;
import com.scs.application.modules.base.mapper.HospitalWarehouseMapper;
import com.scs.application.modules.base.request.HospitalWarehouseQueryRequest;
import com.scs.application.modules.base.service.HospitalWarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 医院收货地点 service 实现
 */
@Slf4j
@Service
public class HospitalWarehouseServiceImpl extends BaseServiceImpl<HospitalWarehouseMapper, HospitalWarehouse> implements HospitalWarehouseService {


    @Override
    public List<HospitalWarehouse> list(HospitalWarehouseQueryRequest request) {
        List<HospitalWarehouse> list = this.list(Wrappers.<HospitalWarehouse>query()
                .eq(request.getHospitalId() != null, "hospital_id", request.getHospitalId())
                .orderByDesc("gmt_modified")
        );
        return list;
    }

    @Override
    public IPage<HospitalWarehouse> page(HospitalWarehouseQueryRequest request) {
        if (UserUtils.isHospital()) {
            request.setHospitalId(Long.valueOf(UserUtils.getHospitalId()));
        }
        IPage page = this.page(request.getPage(), Wrappers.<HospitalWarehouse>query()
                .eq(request.getHospitalId() != null, "hospital_id", request.getHospitalId())
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .like(StringUtils.isNotBlank(request.getCode()), "code", request.getCode())
                .orderByDesc("gmt_modified")
        );
        return page;
    }

    @Override
    public boolean add(HospitalWarehouse entity) {
        int count = this.count(Wrappers.<HospitalWarehouse>query()
                .eq("hospital_id", UserUtils.getHospitalId())
                .and(o -> o.eq("code", entity.getCode()).or().eq("name", entity.getName()))
        );
        return count > 0 ? false : this.saveCascadeById(entity);

    }

    @Override
    public boolean update(HospitalWarehouse entity) {
        int count = this.count(Wrappers.<HospitalWarehouse>query()
                .eq("hospital_id", UserUtils.getHospitalId())
                .ne("id", entity.getId())
                .and(i -> i.eq("code", entity.getCode()).or().eq("name", entity.getName()))
        );
        return count > 0 ? false : this.updateCascadeById(entity);
    }

    /**
     * 根据仓库名查询仓库id
     */
    @Override
    public HospitalWarehouse findByWarehouseName(String warehouseName, String hospitalId) {
        List<HospitalWarehouse> list = this.baseMapper.findByWarehouseName(warehouseName, hospitalId);
        return list.isEmpty() ? null : list.get(0);
    }


}
